const DB = require('app/db.js');
import { monStorage, Storage } from 'clientConfig/util/StoreData';
import{order} from 'clientConfig/util/queryurlfield';
class FileData {

    upFiles = (params, data, ele, callback) => {
        // debugger
        let t = this;
        let upfileName = [];
        ele.style.opacity = 1;
        ele.style.width = "50%";
        var url = "";
        if(window.isBp){
            url = "/its/upload_file?proj_name=";
        }else{
            url = "http://120.55.85.1:8091/its/upload_file?proj_name=";
        }
        fetch(url + params.name + "&file_uper=" + Storage.get("user.id") + "&file_uper_name=" + monStorage.get('user.nickname') + "&folder_id=" + params.folder_id + "&file_version=" + params.version, {
            method: "POST",
            body: data
        }).then(function (response) {
            return response.json();
        }).then(function (result) {
            ele.style.width = "100%"
            setTimeout(function () {
                ele.style.opacity = 0
            }, 700);
            setTimeout(function () {
                ele.style.width = 0
            }, 1000);
            upfileName.push({ fileName: result.content.file_name, fileId: result.content.file_id, file_version: result.content.file_version })
            callback(upfileName)
        }, function (error) {

        })
    }
    delFiles = (id, callback) => {
        let t = this
        DB.File.delete_file({
            file_id: id,
            id:Storage.get('user.id')
        })
            .then((content) => { // 成功
                callback(id)
                // for (var i = 0; i < upfileName.length; i++) {
                //     if (upfileName[i].fileId == id) {
                //         upfileName.splice(i, 1)
                //     }
                // }
                // upfileName = upfileName
            })
            .catch((error) => { // 失败 or 有异常被捕获

            });
    }
    fetchFilePaths = (params, cb) => {
        let t = this;
        DB.File.get_filelists({
            folder_id: params.folder_id,
            id:Storage.get('user.id'),
            type: 1
        })
            .then((content) => {
                content = content.file_list;
                let obj = {};
                let list = [];
                if (content.length > 0) {
                    for (let item of content) {
                        obj.folder = item.file_folder;//是否为文件夹true
                        obj.process_id = item.process_id;//关联的审批单-根目录时含有，其他可省略
                        obj.uploader = item.file_uper;//文件上传者id
                        obj.file_uper_name = item.file_uper_name//文件上传人
                        // obj.folder_name = item.folder_name;//文件夹名称
                        obj.fileName = item.folder_name;//文件夹名称
                        obj.date = item.file_uptime;//文件夹创建时间
                        obj.folder_id = item.folder_id;//文件夹id
                        /*---------------*/
                        obj.fileSize = '---'
                        obj.title = item.folder_name;
                        obj.imgUrl = "../../../../../../assets/img/checknohook.png"
                        switch (obj.fileName) {
                            case "招标":
                            obj.value = 1;
                                break;
                            case "实施":
                            obj.value = 2;
                                break;
                            case "验收":
                            obj.value = 3;
                                break;
                            case "维护":
                            obj.value = 4;
                                break;
                        }
                        list.push(obj)
                        obj = {};          
                    }
                }
                if(list.length==4){
                    list.sort(order)
                }
                cb(list);
                // t.updateComponent();
            })
            .catch((error) => {

            });
    }
}
module.exports = FileData;